<?php
// Init owncloud

require_once(OC_App::getAppPath('resumes') . "/lib/resume.php");

OCP\JSON::callCheck();
$companyid = 0;
if(!OC_User::isLoggedIn()) {
   $app = PFactory::getApplication(); 
   $companyid = $app->getUserState('resumeshare_companyid', 0); 
   if(!$companyid) {
      echo '您需要先登录';
      exit;
   }
} else {
   if (PFactory::isJobseekerUser()) {
      echo '您不是企业用户';
      exit;
   } else {
      $user = PFactory::getMasterUser();
      $companyid = $user->id;
   }
}

$persontestDetail = array();

$uname = JRequest::getString('uname', '');
$jobseeker_id = JRequest::getInt('jobseeker_id', 0);

$model = new PResumesModelResume();
$persontestResultInfo = $model->getPersontestResultInfo($companyid, $jobseeker_id);

if(empty($persontestResultInfo)) {
   echo '没有详细信息';
   exit;
}


$persontestResult = $persontestResultInfo->result;

if (!empty($persontestResult)) {
   if (isset($persontestResult['alias']) && isset($persontestResult['result']) && isset($persontestResult['details'])) {
      $p_start_time = $p_end_time = 0;
      if(!isNullDate($persontestResultInfo->start_time))
         $p_start_time = PFactory::getDate($persontestResultInfo->start_time)->toUnix();
      if(!isNullDate($persontestResultInfo->end_time))
         $p_end_time   = PFactory::getDate($persontestResultInfo->end_time)->toUnix();
      $usedtime = $p_end_time - $p_start_time;
      $usedtime = ($p_start_time!=0 && $p_end_time!=0) ? $usedtime : 300; /* need a reasonable time for old data */
      $usedtime = ceil($usedtime/60) . '分钟';
      $persontestHelperLink = JURI::base().'helps/doc/product/whatcloudforyou.html#alltest';
      if(empty($persontestResult['result'])) { ?>
          <div class='modal-header'>
             <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
             <h3>性格评测结果</h3>
          </div>
          <div class='modal-body' id="modal_noresult">
             <div class='person_putsumer person_nodataputsumer'>
                <div class='person_putsumer_f yahei'><?=preg_replace('/测试/', '', $persontestResultInfo->sheetTitle)?><a target='blank' href='<?=$persontestHelperLink?>' title='什么是性格评估？点我了解更多' data-placement='left'></a></div>
                <div class='person_putsumer_s yahei large'><i></i><?=$uname?></div>
                <div class='person_putsumer_t yahei'>性格特征不明显<em class='songti'>没有发现有参考价值的数据</em></div>
                <div class='person_putsumer_u'>
                   <div class='person_putsumer_uli medium'><span>提交时间：</span><?=JHtml::_('date', $p_end_time, 'Y-m-d H:i')?></div>
                   <div class='person_putsumer_uli medium'><span>用<em>&nbsp;</em>时：</span><?=$usedtime?></div>
                </div>
             </div>
          </div>
      <?php
          exit;
       }
      
      $sheetalias = $persontestResult['alias'];
      $result     = $persontestResult['result'];
      $details    = $persontestResult['details'];
      $output = array();
      switch ($sheetalias) {
         case 'ENNEAGRAM':                  
         case 'MBTI':           
            $persontestAnswer = $model->getPersontestAnswer($sheetalias, $result);            
            if($sheetalias == 'ENNEAGRAM' ){
               ksort($details);  
               $temp = array_values($details);
               $minvalue = min($temp); 
               if($minvalue <= 0){
                  foreach ($temp as &$item) {
                     $item += abs($minvalue) + 5;
                  }                 
               }  
               $persontestDetail['cvs1'] = $temp;
               $persontestDetail['labels'] = array("'完美型'","'全爱型'","'成就型'","'艺术型'","'智慧型'","'忠诚型'","'丰富型'","'领袖型'","'和平型'");
               $persontestTitle = '九型人格';
            }elseif($sheetalias == 'MBTI'){
               $persontestDetail['labels'] = array("'E 外向'","'N 直觉'","'F 情感'","'P 感知'","'I 内向'","'J 判断'","'T 思考'","'S 感觉'");
               $details_key = array('E'=>'','N'=>'','F'=>'','P'=>'','I'=>'','J'=>'','T'=>'','S'=>'');
               $details = array_merge( $details_key,$details);
               $persontestTitle = 'MBTI职业性格';
               $persontestDetail['cvs1'] = array_values($details);
            }
            foreach ($persontestDetail['cvs1']  as $key => $item){
               $output[] = "'" . $item ."'";
            }
            $persontestDetail['tooltips'] = $output;
            $persontestDetail['html'] =                
               "<div class='modal-header'>
                  <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                  <h3>性格评测结果</h3>
                </div>
                <div class='modal-body'>                   
                  <div class='person_newrshow clearfix'>
                     <div class='person_putcanvas pull-left'>
                        <!-- 雷达图用下面的canvas -->
                        <canvas id='cvs1' width='330' height='210'>[No canvas support]</canvas>
                     </div>
                     <div class='person_putsumer pull-right'>
                        <div class='person_putsumer_f yahei'>".$persontestTitle."<a target='blank' href='".$persontestHelperLink."' title='什么是性格评估？点我了解更多' data-placement='left'></a></div>
                        <div class='person_putsumer_s yahei large'><i></i>".$uname."</div>
                        <div class='person_putsumer_t yahei'>".$persontestAnswer->p_shorttitle."</div>
                        <div class='person_putsumer_u'>
                           <div class='person_putsumer_uli medium'><span>提交时间：</span>".JHtml::_('date', $p_end_time, 'Y-m-d H:i')."</div>
                           <div class='person_putsumer_uli medium'><span>用<em>&nbsp;</em>时：</span>".$usedtime."</div>
                        </div>
                     </div>
                  </div>
                  <div class='person_newrtitle yahei'>".$persontestAnswer->p_shorttitle."</div>
                  <div class='person_modalcontent'>".$persontestAnswer->text."</div>
                </div>";
            break;

         case 'HOLLAND':
            $resultText = '';
            $contesntText = '';
            $strengthText = '';
            $titleText = '';
            $patternArr = str_split($result);
            $hAnswers = $model->getPersontestAnswer($sheetalias, $patternArr);
            $aCount = count($hAnswers);
            $find = array('（S）', '（E）', '（C）', '（R）', '（I）', '（A）');
            
            foreach ($hAnswers as $key => $hAnswer) {
               $pAlias = $patternArr[$key];
               $hCount = $details[$pAlias];
               if ($hCount >= 7 && $hCount <= 10) {
                  $strengthText = '强烈的';
               } elseif ($hCount >= 4 && $hCount <= 6) {
                  $strengthText = '较明显的';
               } else {
                  $strengthText = '稍微的';
               }
               
               $resultText .= $strengthText.str_ireplace($find, '', $hAnswer->p_title).'倾向'.($key < ($aCount -1) ? '、' : '');
               $contesntText .= '<b>'.$hAnswer->p_title.'</b><br/>'.$hAnswer->text;
               $titleText .= str_ireplace($find, '', $hAnswer->p_title).($key < ($aCount -1) ? '+' : '');
            }
            $persontestDetail['labels'] = array("'R 现实型'","'I 研究型'","'A 艺术型'","'S 社会型'","'E 企业型'","'C 传统型'");
            $details_key = array('R'=>'','I'=>'','A'=>'','S'=>'','E'=>'','C'=>'');       
            $details = array_merge($details_key,$details);
            $persontestDetail['cvs1'] = array_values($details);
            foreach ($persontestDetail['cvs1']  as $key => $item){
               $output[] = "'" . $item ."'";
            }
            $persontestDetail['tooltips'] = $output;
            $persontestDetail['html'] = "
               <div class='modal-header'>
                  <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                  <h3>性格评测结果</h3>
               </div>
               <div class='modal-body'>                    
                  <div class='person_newrshow clearfix'>
                     <div class='person_putcanvas pull-left'>
                        <!-- 雷达图用下面的canvas -->
                        <canvas id='cvs1' width='330' height='210'>[No canvas support]</canvas>
                     </div>
                     <div class='person_putsumer pull-right'>
                        <div class='person_putsumer_f yahei'>霍兰德职业兴趣<a target='blank' href='".$persontestHelperLink."' title='什么是性格评估？点我了解更多' data-placement='left'></a></div>
                        <div class='person_putsumer_s yahei large'><i></i>".$uname."</div>
                        <div class='person_putsumer_t yahei'>".$result."(".$titleText.")</div>
                        <div class='person_putsumer_uli' style='font-style: normal; margin-top: 12px; font-size: 13px;'>".$resultText."</div>
                        <div class='person_putsumer_u'>
                           <div class='person_putsumer_uli medium'><span>提交时间：</span>".JHtml::_('date', $p_end_time, 'Y-m-d H:i')."</div>
                           <div class='person_putsumer_uli medium'><span>用<em>&nbsp;</em>时：</span>".$usedtime."</div>
                        </div>                     
                     </div>
                  </div>
                  <div class='person_newrtitle yahei'>".$result."(".$titleText.")</div>
                  <div class='person_modalcontent'>".$contesntText."</div>
                </div>";    
            break;
         case 'COMMUNICATE':
         case 'EQ':
         case 'STRESS':
              $persontestAnswer = $model->getPersontestAnswer($sheetalias, $result); 
              $subhtml = '';
              $position = 0;
              $score = !empty($details['SCORE']) ? $details['SCORE'] : 0;
              if($sheetalias == 'COMMUNICATE'){
                 $position = ($score-10)/30*100;
                 $subhtml = " 
                    <div class='plinechart'>  
                       <div class='userpoints_wrap'>
                          <div class='userpoints yahei' style='margin-left:-30px; left:".$position."%;'>".$score."</div>
                       </div>   
                       <div class='lineprogress clearfix'> 
                          <div class='linecolor_pick pull-left' style='width:33.33%'></div>
                          <div class='linecolor_blue pull-left' style='width:33.33%'></div> 
                          <div class='linecolor_green pull-left' style='width:33.34%'></div>   
                       </div>   
                       <div class='lineprogress_num clearfix'>
                          <div style='left:-2%'>10</div>   
                          <div style='left:31%'>21</div>   
                          <div style='left:64%'>31</div>   
                          <div style='left:97%'>40</div>   
                       </div>   
                       <div class='labeltips'>
                          <div class='clearfix'>  
                             <div class='labeltips_c_2'><i></i>不善表达(10~20分)</div>  
                             <div class='labeltips_c_3'><i></i>沟通良好(21~30分)</div>  
                          </div>
                          <div class='clearfix'>  
                             <div class='labeltips_c_4'><i></i>善于交际(31~40分)</div>  
                          </div>   
                       </div>   
                    </div>";
              $persontestTitle = '人际沟通能力';
              }elseif($sheetalias == 'EQ'){
                 if($score>250){
                    $position = 99;
                 }else{
                    $position = $score*2/250*100;
                 }
                 $subhtml="
                    <div class='plinechart'>   
                       <div class='userpoints_wrap'> 
                          <div class='userpoints yahei' style='margin-left:-30px; left:".$position."%;'>".($score*2)."</div>  
                       </div>   
                       <div class='lineprogress clearfix'>
                          <div class='linecolor_gray pull-left' style='width:36%'></div>
                          <div class='linecolor_pick pull-left' style='width:15.2%'></div> 
                          <div class='linecolor_blue pull-left' style='width:11.6%'></div> 
                          <div class='linecolor_green pull-left' style='width:36%'></div>   
                       </div>   
                       <div class='lineprogress_num clearfix'>   
                          <div style='left:34%'>90</div>   
                          <div style='left:46%'>130</div>  
                          <div style='left:60%'>160</div>  
                       </div>   
                       <div class='labeltips'> 
                          <div class='clearfix'>  
                             <div class='labeltips_c_1'><i></i>EQ较低(90分以下)</div>   
                             <div class='labeltips_c_2'><i></i>EQ一般(90~129分)</div> 
                          </div>   
                          <div class='clearfix'>  
                             <div class='labeltips_c_3'><i></i>EQ较高(130~159分)</div>   
                             <div class='labeltips_c_4'><i></i>EQ高手(160分以上)</div>   
                          </div>   
                       </div>   
                    </div>";
                 $persontestTitle = '情商';
              }elseif($sheetalias == 'STRESS'){
                 $position = (15-$score)/15*100;
                 $subhtml = "
                    <div class='plinechart'> 
                       <div class='userpoints_wrap'> 
                          <div class='userpoints yahei' style='margin-left:-30px; left:".$position."%;'>".$score."</div>
                       </div>
                       <div class='lineprogress clearfix'>  
                          <div class='linecolor_gray pull-left' style='width:13.3333333333%'></div> 
                          <div class='linecolor_pick pull-left' style='width:33.3333333333%'></div> 
                          <div class='linecolor_blue pull-left' style='width:33.3333333333%'></div> 
                          <div class='linecolor_green pull-left' style='width:20%'></div>   
                       </div> 
                       <div class='lineprogress_num clearfix'>   
                          <div style='left:-2%'>15</div>   
                          <div style='left:11%'>13</div>
                          <div style='left:45%'>8</div> 
                          <div style='left:79%'>3</div>
                          <div style='left:98%'>0</div> 
                       </div>   
                       <div class='labeltips'> 
                          <div class='clearfix'>  
                             <div class='labeltips_c_1'><i></i>解压失败者(13~15分)</div> 
                             <div class='labeltips_c_2'><i></i>产生逆反心理(8~12分)</div> 
                          </div>   
                          <div class='clearfix'>
                             <div class='labeltips_c_3'><i></i>过分紧张(3~7分)</div> 
                             <div class='labeltips_c_4'><i></i>解压能力强(0~2分)</div>   
                          </div>   
                       </div>   
                    </div> ";                   
                 $persontestTitle = '抗压能力';
              }
              $persontestDetail['html']="
                 <div class='modal-header'>
                    <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                    <h3>性格评测结果</h3>
                 </div>
                 <div class='modal-body'>                    
                 <div class='person_newrshow clearfix'>
                    <div class='person_putcanvas pull-left'>".$subhtml."</div>
                    <div class='person_putsumer pull-right'>
                       <div class='person_putsumer_f yahei'>".$persontestTitle."<a target='blank' href='".$persontestHelperLink."' title='什么是性格评估？点我了解更多' data-placement='left'></a></div>
                       <div class='person_putsumer_s yahei large'><i></i>".$uname."</div>
                       <div class='person_putsumer_t yahei'>".$persontestAnswer->p_shorttitle."</div>
                       <div class='person_putsumer_u'>
                          <div class='person_putsumer_uli medium'><span>提交时间：</span>".JHtml::_('date', $p_end_time, 'Y-m-d H:i')."</div>
                          <div class='person_putsumer_uli medium'><span>用<em>&nbsp;</em>时：</span>".$usedtime."</div>
                       </div>                     
                    </div>
                 </div>
                 <div class='person_newrtitle yahei'>".$persontestAnswer->p_shorttitle."</div>
                 <div class='person_modalcontent'>".$persontestAnswer->text."</div>
                 </div>";
         break;
      }

      if(!empty($persontestDetail['html'])){
         echo $persontestDetail['html'];
      }
   }
} else {
   echo '系统错误';
   exit;
}
?>

<?php if(!empty($persontestDetail['cvs1'])):?>
<script type="text/javascript">
   $(document).ready(function(){
      if($("#cvs1").length>0){
         var radar = new RGraph.Radar('cvs1', [<?=implode(',', $persontestDetail['cvs1'])?>])
         .set('labels', [<?=implode(',', $persontestDetail['labels'])?>])
         .set('labels.axes.bold',true)
         .set('text.size',12)
         .set('tooltips',[<?=implode(',', $persontestDetail['tooltips'])?>])
         .set('background.circles.poly', true)
         .set('background.circles.spacing', 30)
         .set('colors', ['rgba(255,105,0,0.25)','rgba(255,255,0,0.25)'])
         .set('axes.color', 'transparent')
         .set('highlights', true)
         .set('strokestyle', ['red','black'])
         .set('highlights.radius',2)
         .set('linewidth',0.5)
         .on('beforedraw', function (obj)
         {
           RGraph.clear(obj.canvas, 'white');
         })
         .grow();
      }
})
</script>
<?php endif;?>